package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Car;
import com.ruoyi.system.domain.CarMenu;
import com.ruoyi.system.service.ICarFileService;
import com.ruoyi.system.service.ICarMenuService;
import com.ruoyi.system.service.ICarService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 车辆车款Controller
 *
 * @author 刘金磊
 * @date 2022-03-12
 */
@Controller
@RequestMapping("/system/car")
public class CarController extends BaseController {
    private String prefix = "system/car";

    @Autowired
    private ICarService carService;
    @Resource
    private ICarFileService iCarFileService;

    @RequiresPermissions("system:car:view")
    @GetMapping()
    public String car() {
        return prefix + "/car";
    }

    /**
     * 查询车辆车款列表
     */
    @RequiresPermissions("system:car:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Car car) {
        startPage();
        List<Car> list = carService.selectCarList(car);
        return getDataTable(list);
    }

    /**
     * 导出车辆车款列表
     */
    @RequiresPermissions("system:car:export")
    @Log(title = "车辆车款", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Car car) {
        List<Car> list = carService.selectCarList(car);
        ExcelUtil<Car> util = new ExcelUtil<Car>(Car.class);
        return util.exportExcel(list, "车辆车款数据");
    }

    /**
     * 新增车辆车款
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存车辆车款
     */
    @RequiresPermissions("system:car:add")
    @Log(title = "车辆车款", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Car car) {
        car.setCreateBy(getLoginName());
        car.setUpdateBy(car.getCreateBy());
        return toAjax(carService.insertCar(car));
    }

    /**
     * 修改车辆车款
     */
    @RequiresPermissions("system:car:edit")
    @GetMapping("/edit/{carId}")
    public String edit(@PathVariable("carId") Long carId, ModelMap mmap) {
        Car car = carService.selectCarByCarId(carId);
        mmap.put("car", car);
//        mmap.put("images", iCarFileService.getFiles(carId, "car_info"));
//        mmap.put("files", iCarFileService.getFiles(carId, "car_info", "file"));
        return prefix + "/edit";
    }

    /**
     * 修改保存车辆车款
     */
    @RequiresPermissions("system:car:edit")
    @Log(title = "车辆车款", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Car car) {
        car.setUpdateBy(getLoginName());
        return toAjax(carService.updateCar(car));
    }

    /**
     * 删除车辆车款
     */
    @RequiresPermissions("system:car:remove")
    @Log(title = "车辆车款", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(carService.deleteCarByCarIds(ids));
    }

    /**
     * 车辆详细信息
     */
    @GetMapping("/tree")
    public String tree() {
        return prefix + "/tree";
    }

    /**
     * 加载部门列表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData() {
        List<Ztree> ztrees = iCarMenuService.selectDeptTree(new CarMenu());
        return ztrees;
    }

    @Resource
    private ICarMenuService iCarMenuService;
}
